<?php
function insert_scraper($args) {
	require_once('Zend/Cache.php');
	// Configure our Zend Framework cache
	$frontendOptions = array('lifetime' => 1800);
	$backendOptions = array('cache_dir' => './cache/');
	$cache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions);

	$ch = curl_init($args['url']);  // create a new cURL resource
	if ($ch === FALSE) {
		echo "Error: Bad URL";
		return;
	}
	else {
		if (isset($args['id']))
			$id = $args['id'];
		else
			$id = md5($args['url']);
	}

	if(!$cache->start($id)) {
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$data = curl_exec($ch);
		if (preg_match($args['pattern'], $data, $matches)) {
			// Fix URLs
			$url = parse_url($args['url']);
			$out = preg_replace('@href="\/(.*?)"@isu', 'href="' . $url['scheme'] . '://' . $url['host'] . '/$1"', $matches[1]);
			// Output fixed HTML using Tidy if possible
			if (function_exists('tidy_repair_string'))
				echo tidy_repair_string($out, array('output-xhtml' => true, 'show-body-only' => true));
			else
				echo $out;
		}
		else
			echo "No Data";
		$cache->end();
	}
	curl_close($ch);
}
?>